package com.feiyu.paymentAndcredit.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.feiyu.paymentAndcredit.entity.DealEntity;
import com.feiyu.paymentAndcredit.service.DealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.feiyu.paymentAndcredit.entity.ShopEntity;
import com.feiyu.paymentAndcredit.service.ShopService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;


/**
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-03 12:36:05
 */
@RestController
public class ShopController {
    @Autowired
    private ShopService shopService;

    @Autowired
    private DealService dealService;

    /**
     * 店铺查看钱包
     */
    @PostMapping("/searchShopWallet")
    public R searchShopWallet(@RequestBody ShopEntity shopEntity) {
        ShopEntity shop = shopService.getById(shopEntity.getShopId());
        if (shop == null) {
            return R.error("查看失败").put("data", null);
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("shopId", shop.getShopId());
        hashMap.put("userId", shop.getUserId());
        hashMap.put("money", shop.getMoney());
        return R.ok("查看成功").put("data", hashMap);
    }

    /**
     * 店铺充值账户
     */
    @PostMapping("/addShopWalletAmount")
    public R addShopWalletAmount(@RequestBody ShopEntity shopEntity) {
        ShopEntity shop = shopService.getById(shopEntity.getShopId());
        if (shop == null) {
            return R.error("未查询到该用户").put("data", null);
        }
        float money = shopEntity.getMoney();
        float newShopMoney = shop.getMoney() + money;
        shopEntity.setMoney(newShopMoney);
        shopService.updateById(shopEntity);
        shop = shopService.getById(shopEntity.getShopId());
        DealEntity dealEntity = new DealEntity();
        dealEntity.setMainTraderId(shop.getUserId());
        dealEntity.setMinorTrader("飞鱼商城平台");
        dealEntity.setDealTime(new Date());
        dealEntity.setDealAmount(money);
        dealEntity.setDealIntro("充值账户");
        dealService.save(dealEntity);
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("shopId", shop.getShopId());
        hashMap.put("userId", shop.getUserId());
        hashMap.put("money", shop.getMoney());
        return R.ok("操作成功").put("data", hashMap);
    }



}
